#include "sqqueue.h"

Psqueue createEmptyQueue(int num)
{
    Psqueue queue = (Psqueue) malloc(sizeof(struct stQueue));

    if (queue != NULL)
    {
        queue->buffer = (ELEMTYPE *) malloc(sizeof(ELEMTYPE)*num);
        if (queue->buffer != NULL)
        {
            queue->MAXNUM = num;
            queue->length = 0;
            queue->head = 0;
            queue->rear = 0;
            return queue;
        }

        free(queue);
    }
    
    return NULL;
}

int enqueue(Psqueue queue, ELEMTYPE x)
{
    if (queue == NULL)
    {
        return -1;
    }
    
    if (isFull(queue) == 1)
    {
        return 0;
    }
    
    queue->buffer[queue->rear] = x;
    queue->rear = (queue->rear + 1) % queue->MAXNUM;
    queue->length = queue->length + 1;

    return 1;
}

int dequeue(Psqueue queue)
{
    if (queue == NULL)
    {
        return -1;
    }
    
    if (isEmpty(queue) == 1)
    { return 0;
    }
    
    queue->length = queue->length - 1;
    queue->head = (queue->head + 1) % queue->MAXNUM;
    
    return 1;
}

int frontqueue(Psqueue queue, ELEMTYPE *x)
{
    if (queue == NULL)
    {
        return -1;
    }
    
    if (isEmpty(queue) == 1)
    {
        return 0;
    }
    
    *x = queue->buffer[queue->head];
    return 1;
}

int empty(Psqueue queue)
{
    if (queue == NULL)
    {
        return -1;
    }
    
    queue->head = 0;
    queue->rear = 0;
    queue->length = 0;
    return 1;
}

int length(Psqueue queue)
{
    if (queue == NULL)
    {
        return -1;
    }
    
    return queue->length;
}
int isEmpty(Psqueue queue)
{
    if (queue == NULL)
    {
        return -1;
    }
    
    // method 1
    //return queue->length == 0;
    // method 2
    return queue->head == queue->rear;
}

int isFull(Psqueue queue)
{
    if (queue == NULL)
    {
        return -1;
    }
    
    // method 1
    //return queue->length == queue->MAXNUM;
    // method 2
    return (queue->rear+1)%queue->MAXNUM == queue->head;
}

int destroy(Psqueue *queue)
{
    if (*queue == NULL)
    {
        return -1;
    }
    
    free((*queue)->buffer);
    free(*queue);
    return 1;
}
int printQueue(Psqueue queue)
{
    POSITION p;
    char *index;

    int flag = 0;
    if (queue == NULL)
    {
        return -1;
    }
    
    p = 0;

    index = (char *) malloc(sizeof(char)*queue->MAXNUM);

    if (index != NULL)
    {
        memset(index, ' ', queue->MAXNUM);
        index[queue->rear] = 'R';
        index[queue->head] = 'H';
    }

    if (queue->rear < queue->head)
    {
        flag = 1;
    }

    for (p = 0; p < queue->MAXNUM; p++)
    {
        if (flag && p >= queue->rear && p < queue->head)
        {
            printf("* ");
        }
        else if (!flag && (p < queue->head || p >= queue->rear))
        {
            printf("* ");
        }
        else
        {
            printf("%-2c", queue->buffer[p]);
        }
    }

    printf("\n");
    if (index != NULL)
    {
        for (p = 0; p < queue->MAXNUM; p++)
        {
            printf("%-2c", index[p]);
        }
    }
    printf("\n");
    
    if (index != NULL)
    {
        free(index);
        index = NULL;
    }
    return 1;
}
